//index.js
const app = getApp();
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    defaultColor: '',
    borderColor: '',
    cnSchoolId: '',
    cnSchoolName: '',
    ukSchoolArray: [],
    ukSchoolIndex: 0,
    ukSchoolId: '',
    ukSchoolName: '',
    majorArray: [],
    majorIndex: 0,
    major: ''
  },
  onLoad: function () {
    self = this;
    self.getStyle();
    self.getMajors();
    self.getUkSchools();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  getMajors() {
    self.setData({
      majorArray: app.globalData.majors
    })
  },
  getUkSchools() {
    wx.request({
      url: host + '/lx/search/ukSchools',
      success: function (res) {
        if (res.data.status == 200) {
          var list = res.data.info;
          list.unshift({
            id: 0,
            name: "暂不选择"
          })
          self.setData({
            ukSchoolArray: list
          })
        } else {
          wx.showToast({
            title: '获取信息失败',
            icon: 'none'
          })
        }
      }
    })
  },
  formSubmit(e) {
    if (self.data.cnSchoolId == '') {
      wx.showToast({
        title: '请先选择中国大学',
        icon: 'none',
        duration: 2500
      })
      return;
    }
    if (self.data.major == '') {
      wx.showToast({
        title: '请选择想要申请的专业方向',
        icon: 'none',
        duration: 2500
      })
      return;
    }
    // if (self.data.ukSchoolId != '') {
    //   wx.navigateTo({
    //     url: '/pages/result/result?cnSchoolId=' + self.data.cnSchoolId + "&ukSchoolId=" + self.data.ukSchoolId + "&major=" + self.data.majorIndex
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: '/pages/ukCollege2/ukCollege2?cnSchoolId=' + self.data.cnSchoolId + '&major=' + self.data.majorIndex
    //   })
    // }
    wx.request({
      url: host + '/lx/search/countSearch'
    })
    wx.navigateTo({
      url: '/pages/ukCollege2/ukCollege2?cnSchoolId=' + self.data.cnSchoolId + '&major=' + self.data.majorIndex + "&ukSchoolId=" + self.data.ukSchoolId
    })
  },
  goCnCollege(e) {
    wx.navigateTo({
      url: '/pages/cnCollege/cnCollege'
    })
  },
  selectMajor(e) {
    var index = e.detail.value;
    this.setData({
      majorIndex: index,
      major: self.data.majorArray[index]
    })
    // if (index == 4 || index == 5 || index == 6 || index == 7) {
    //   this.setData({
    //     major: self.data.major.replace("　", "<br />")
    //   })
    // }
  },
  selectUkSchool(e) {
    if (e.detail.value == 0) {
      this.setData({
        ukSchoolId: '',
        ukSchoolName: ''
      })
    } else {
      this.setData({
        ukSchoolId: self.data.ukSchoolArray[e.detail.value].id,
        ukSchoolName: self.data.ukSchoolArray[e.detail.value].name
      })
    }
  },
  goUkCollege(e) {
    // if (self.data.cnSchoolId == '') {
    //   wx.showModal({
    //     title: '提示',
    //     content: '请先选择中国大学',
    //     showCancel: false
    //   })
    //   return;
    // }
    wx.navigateTo({
      // url: '/pages/ukCollege/ukCollege?cnSchoolId=' + self.data.cnSchoolId
      url: '/pages/ukCollege/ukCollege'
    })
  }
})
